package com.personal.listener;

import com.personal.mapper.TApplyMapper;
import com.personal.mapper.TCourseMapper;
import com.personal.mapper.TMemberMapper;
import com.personal.mapper.TSectionMapper;
import com.personal.pojo.TCourse;
import com.personal.service.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebListener
public class TypeListener implements ServletContextListener {
    //1.从容器中获取对象
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-*.xml");
    TMemberMapper mapper = (TMemberMapper) ac.getBean("TMemberMapper");
    TCourseMapper courseMapper = (TCourseMapper) ac.getBean("TCourseMapper");
    TApplyMapper applyMapper = (TApplyMapper) ac.getBean("TApplyMapper");

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //将类型表加入全局范围，给select-option。
        //1.1获取卡类型service对象
        CardService cardService = (CardService) ac.getBean("cardServiceImp");
        //1.2获取节课
        TSectionMapper sectionMapper = (TSectionMapper) ac.getBean("TSectionMapper");
        //2.放入全局作用域
        List cardlist = cardService.selectAllCard();
        servletContextEvent.getServletContext().setAttribute("cardlist",cardlist);
        List sectionList = sectionMapper.selectByExample();
        servletContextEvent.getServletContext().setAttribute("sectionList",sectionList);
        //将会员卡到期的全部结束掉，将states置为0
        endMem();
        //将课程开始的置为2，课程结束的置为0
        endCourse();
        //课程结束将报名状态置为0
//        endApply();
        //为所有课程统计报名人数
        addApplyMem();
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
    //将过期会员状态置为0
    public void endMem(){
        //获取系统当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(date);
        //将会员卡到期的会员状态置为0
        mapper.endDate(dateStr);
    }
    //课程过期状态置为0，开始置为2
    public void endCourse(){
        //获取系统当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(date);
        //将开始的课程状态置为2
        courseMapper.starCourse(dateStr);
        //将结束的课程状态置为0
        courseMapper.endCourse(dateStr);
    }
    //课程过期状态置为0，开始置为2
    public void endApply(){
        //得到所有结束状态的课程
        List list = courseMapper.selectCourseEnd();
        //结束报名状态
        applyMapper.delApply(list);
    }
    public void addApplyMem(){
        //获取所有非结束状态的课程
        List list = courseMapper.selectNotEnd();
        //遍历将报名人数统计出来，修改课程报名人数
        for (Object c:list) {
            TCourse course = (TCourse) c;
            //统计人数
            int row = applyMapper.countMem(course.getCourseid());
            //修改人数
            courseMapper.editApply(row,course.getCourseid());
        }
    }
}
